package com.fy56.platform.logistics.controller;

import com.fy56.platform.logistics.common.*;
import com.fy56.platform.logistics.model.Company;
import com.fy56.platform.logistics.model.News;
import com.fy56.platform.logistics.model.Vip;
import com.fy56.platform.logistics.service.wx.ILogisticsSlaveBasisService;
import com.fy56.platform.logistics.service.wx.IVipService;
import com.fy56.platform.logistics.utils.LogUtils;
import com.fy56.platform.logistics.utils.SessionUtil;
import com.fy56.platform.logistics.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

//import com.fy56.platform.logistics.service.wx.ILogisticsSlaveBasisService;

/**
 * Created by Ning on 2018/4/28.
 */
@RestController
@RequestMapping("/controller/vipmember")
public class VipController extends BaseController{
    @Autowired
    private IVipService iVipService;

    @Autowired
    private ILogisticsSlaveBasisService iLogisticsSlaveBasisService;

    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> findList(Vip vip){
       List<Vip> list = iVipService.select(vip);
       ResultModel r = ResultModel.ok(list);
       return new ResponseEntity<>(r,HttpStatus.OK);
    }

    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> get(Long keyId){
        Vip c = iVipService.getVipById(keyId);
        ResultModel r = ResultModel.ok(c);
        return  new ResponseEntity<>(r, HttpStatus.OK);
    }

    @RequestMapping(value = "/getAuditStatusPass", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> getAuditStatusPass(Long keyId, HttpServletRequest request) {
        ResultModel resultModel = null;
        Vip vip = null;
        try{
            if (keyId != null && (keyId.intValue() !=0)) {
                vip = new Vip();
                vip.setId(keyId);
                vip.setAuditStatus(1);
                vip.initUpdate(SessionUtil.getSessionUser(request));

                //执行删除操作
                resultModel = iVipService.updateAuditStatus(vip);

                //保存系统业务日志
                LogUtils.saveLog(request,"成功",keyId.toString(),null,"VipManger","delete","审核", keyId.toString());
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("审核失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",keyId.toString(),null,"VipManger","delete","审核", keyId.toString());
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            vip = null;
        }
    }

    @RequestMapping(value = "/getAuditStatusBh", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> getAuditStatusBh(Long keyId, HttpServletRequest request) {
        ResultModel resultModel = null;
        Vip vip = null;
        try{
            if (keyId != null && (keyId.intValue() !=0)) {
                vip = new Vip();
                vip.setId(keyId);
                vip.setAuditStatus(2);
                vip.initUpdate(SessionUtil.getSessionUser(request));

                //执行删除操作
                resultModel = iVipService.updateAuditStatus(vip);

                //保存系统业务日志
                LogUtils.saveLog(request,"成功",keyId.toString(),null,"VipManger","delete","驳回", keyId.toString());
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("驳回失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",keyId.toString(),null,"VipManger","delete","驳回", keyId.toString());
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            vip = null;
        }
    }

    /**
     * 批量审核
     *
     * @param keyIds
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchAuditStatusPass", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> batchAuditStatusPass(String keyIds, HttpServletRequest request) {
        try{
            if (StringUtil.isNotEmpty(keyIds)) {

                //构建删除对象（服务器对象的集合）
                LinkedList<Vip> list = new LinkedList<Vip>();
                Vip vip = null;
                for(String id : keyIds.split(",")){
                    if(StringUtil.isNotEmpty(id)){
                        vip = new Vip();
                        vip.setAuditStatus(1);
                        vip.initUpdate(SessionUtil.getSessionUser(request));
                        vip.setId(Long.parseLong(id));
                        list.add(vip);
                    }
                }

                //执行删除操作
                ResultModel resultModel = iVipService.batchAuditStatusPass(new ArrayList<Vip>(list));

                //保存系统业务日志
                LogUtils.saveLog(request,"",keyIds,null,"VipManger","batchDelete","批量审核",null);
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("批量审核失败!"), HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }
    }

    /**
     * 批量驳回
     *
     * @param keyIds
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchAuditStatusBh", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> batchAuditStatusBh(String keyIds, HttpServletRequest request) {
        try{
            if (StringUtil.isNotEmpty(keyIds)) {

                //构建删除对象（服务器对象的集合）
                LinkedList<Vip> list = new LinkedList<Vip>();
                Vip vip = null;
                for(String id : keyIds.split(",")){
                    if(StringUtil.isNotEmpty(id)){
                        vip = new Vip();
                        vip.setAuditStatus(2);
                        vip.initUpdate(SessionUtil.getSessionUser(request));
                        vip.setId(Long.parseLong(id));
                        list.add(vip);
                    }
                }

                //执行删除操作
                ResultModel resultModel = iVipService.batchAuditStatusPass(new ArrayList<Vip>(list));

                //保存系统业务日志
                LogUtils.saveLog(request,"",keyIds,null,"VipManger","batchDelete","批量驳回",null);
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("批量驳回失败!"), HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }
    }

}
